import Store from '@/store'
import Cookies from 'js-cookie'
import { EventBus } from './eventBus'

let lockReconnect = false
// let limitConnect = 3
let timeoutnum = null
function init (cd) {
  let token = Cookies.get('token') || ''
  if (!(typeof WebSocket === 'undefined')) {
    try {
      Store.state.app.WebSocket = new WebSocket(`${window.SITE_CONFIG['socketURL']}?token=${token}`)
      Store.state.app.WebSocket.onopen = function (e) {
        sendHeartBeat.start()
      }
      Store.state.app.WebSocket.onmessage = function (e) {
        sendHeartBeat.start()
        try {
          Store.state.app.webSocketMessage = JSON.parse(e.data)
          let data = JSON.parse(e.data)
          // if (data.alert) {
          //   cd(data)
          // } else if (data.businessType === 'webmsg') {
          //   cd(data)
          // } else if (data.businessType === 'grid') {
          //   EventBus.$emit('sqlagent', data.data)
          // }
          if(data.notifyType === 'webmsg') {
            cd(data)
          }else if (data.notifyType === 'grid') {
            EventBus.$emit('sqlagent', data)
          }
        } catch (error) {

        }
      }
      Store.state.app.WebSocket.onerror = function (e) {
        reconnect()
      }
      Store.state.app.WebSocket.onclose = function (e) {
        if (e.code !== 1000) reconnect()
      }
    } catch (error) {
      clearTimeout(timeoutnum)
      timeoutnum = setTimeout(function () {
        init(cd)
      }, 60000)
    }
  }

  let sendHeartBeat = {
    timeout: 1000 * 30,
    timeoutObj: null,
    severTimeoutObj: null,
    start () {
      const _self = this
      this.timeoutObj && clearTimeout(this.timeoutObj)
      this.severTimeoutObj && clearTimeout(this.severTimeoutObj)
      this.timeoutObj = setTimeout(() => {
        Store.state.app.WebSocket.send('HeartBeat')
        _self.severTimeoutObj = setTimeout(() => {
          Store.state.app.WebSocket.close()
        }, _self.timeout)
      }, this.timeout)
    }
  }
  function reconnect () {
    if (lockReconnect) return
    lockReconnect = true
    // limitConnect--
    timeoutnum && clearTimeout(timeoutnum)
    timeoutnum = setTimeout(function () {
      init(cd)
      lockReconnect = false
    }, 60000)
  }
}
export default init

// var heartCheck = {
//   timeout: 1000 * 50, // 1min
//   timeoutObj: null,
//   reset: function () {
//     clearTimeout(this.timeoutObj)
//     this.start()
//   },
//   start: function () {
//     this.timeoutObj = setTimeout(function () {
//       Store.state.app.WebSocket.send('HeartBeat')
//     }, this.timeout)
//   }
// }

// function init (cd) {
//   if (!(typeof (WebSocket) === 'undefined')) {
//     Store.state.app.WebSocket = new WebSocket(`${window.SITE_CONFIG['socketURL']}?token=${token}`)
//     Store.state.app.WebSocket.onopen = function (e) {
//       heartCheck.start()
//       // setIntervalHeartBeat()
//     }
//     Store.state.app.WebSocket.onmessage = function (e) {
//       heartCheck.reset()
//       cd(JSON.parse(e.data))
//     }
//     Store.state.app.WebSocket.onerror = function (e) {
//       // heartCheck.reset()
//       // Store.state.app.WebSocket = null
//       init(cd)
//     }
//     Store.state.app.WebSocket.onclose = function (e) {
//       // heartCheck.reset()
//       // Store.state.app.WebSocket = null
//       init(cd)
//     }
//   }
// }
